package lifeOfAFish.scenegraph;

import javax.media.opengl.*;
/**
 * A basic transformation node.
 * Note that while there are functions provided for translation,
 * rotation and scaling, only the <i>last</i> transformation specified
 * will be valid. i.e Each new transformation function <i>overwrites</i>
 * the current transformation matrix.
 * 
 * For the time being, if you want to use multiple transformations, just 
 * create mutiple nodes. 
 * @author Erik Bonner
 *
 */
public class sgTransform extends sgNode {
	
	private float[] m = new float[16];
	private boolean m_set = false; 
	
	/**
	 * Constructor. Default transform is a 0 translation. 
	 */
	public sgTransform(){
	}
	
	/**
	 * Render function inherited from sgNode.
	 * Applies transform to current view matrix. 
	 */
	public void render() {
		GL gl = sgGlobals.gl;
		if(!m_set) 
			this.translate(0.0f, 0.0f, 0.0f);
		else {
			gl.glMultMatrixf(m, 0);
		}
			
	}
	
	/**
	 * Apply translation
	 */
	public void translate(float x, float y, float z)
	{
		GL gl = sgGlobals.gl;
		
		// apply translation
		gl.glPushMatrix();
		gl.glLoadIdentity();
		gl.glTranslatef(x, y, z);
		gl.glGetFloatv(GL.GL_MODELVIEW_MATRIX, m, 0);
		gl.glPopMatrix();
		
		m_set = true;
		m_matrix = new sgMatrix3D(m);
	}
	public void translate(float[] v){
		this.translate(v[0], v[1], v[2]);
	}
	                              
	
	/**
	 * Apply rotation
	 */
	public void rotate(float theta, float axis_x, float axis_y, float axis_z)
	{
		// apply rotation
		GL gl = sgGlobals.gl;
		
		// apply translation
		gl.glPushMatrix();
		gl.glLoadIdentity();
		gl.glRotatef(theta, axis_x, axis_y, axis_z);
		gl.glGetFloatv(GL.GL_MODELVIEW_MATRIX, m, 0);
		gl.glPopMatrix();
		
		m_set = true;
		m_matrix = new sgMatrix3D(m);
	}
	
	/** 
	 * Scale
	 */
	public void scale(float sx, float sy, float sz)
	{
		GL gl = sgGlobals.gl;
		
		// apply translation
		gl.glPushMatrix();
		gl.glLoadIdentity();
		gl.glScalef(sx, sy, sz);
		gl.glGetFloatv(GL.GL_MODELVIEW_MATRIX, m, 0);
		gl.glPopMatrix();
		
		m_set = true;
		m_matrix = new sgMatrix3D(m);
	}
	public void scale(float[] v){
		this.scale(v[0], v[1], v[2]);
	}
	       

}
